import java.sql.*;
import javax.swing.*;
import java.rmi.server.*;
import java.rmi.*;



class adhocimpl extends UnicastRemoteObject implements Interface
{
	int k=0;	
	int val,value=0;
	String valu="";
	public adhocimpl()throws RemoteException
	{
	
	}
		public void insert(String query)throws java.rmi.RemoteException
		   {
				try
				{
					
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
		   }
		public int delete() throws java.rmi.RemoteException
		{
			int i=0;
			try
			{
				Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
				Connection con=DriverManager.getConnection("jdbc:odbc:a1");
				Statement st1=con.createStatement();
				Statement st2=con.createStatement();
				st1.executeUpdate("delete *from a1");
				st2.executeUpdate("insert into a1 values(0,0,0,0)");				
			}
			catch (Exception e)
			{
				i=1;
				e.printStackTrace();
			}
			return i;
		}
		public int retrieve1(String query2) throws java.rmi.RemoteException
		{
			int flag=0;
				try
				{
					if (query2.equals("Node2")) 
					{					
					
										
								Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
								Connection con=DriverManager.getConnection("jdbc:odbc:a1");
								Statement st1=con.createStatement();
								Statement st2=con.createStatement();
								ResultSet rs=st1.executeQuery("select * from a1");
								while(rs.next())
								 {
									valu=rs.getString(2);
									val=Integer.parseInt(valu); 
									if(val==1)
									{
										flag=1;
										break;
									}
								 }
								if(flag==1)
								{
									String process="process is going on";
									JOptionPane jp=new JOptionPane();
									jp.showMessageDialog(jp,process);
									System.out.println("value of :" +valu);								
								}										
								else 
								{
									String pro1="RREQ to Node2";
									JOptionPane jp=new JOptionPane();
									jp.showMessageDialog(jp,pro1);
									Thread.sleep(500);

									String pro="RREP from Node2";									
									jp.showMessageDialog(jp,pro);
									st2.executeUpdate("insert into a1 values(1,1,0,0)");								
								}						
						
								
					}
					else if  (query2.equals("Node3")) 
					{
					
							Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
							Connection con=DriverManager.getConnection("jdbc:odbc:a1");
							Statement st1=con.createStatement();
							Statement st2=con.createStatement();
							ResultSet rs=st1.executeQuery("select * from a1");
							while(rs.next())
							{
								valu=rs.getString(3);
								val=Integer.parseInt(valu);
								
								if(val==1)
								{
									flag=1;
									break;
								}
							}
							   if(flag==1)
								{
									String process="process is going on";
									JOptionPane jp=new JOptionPane();
									jp.showMessageDialog(jp,process);
									System.out.println("value of :" +valu);								
								}
								else 
								{
									String pro1="RREQ to Node3";
									JOptionPane jp=new JOptionPane();
									jp.showMessageDialog(jp,pro1);
									Thread.sleep(1000);

									String pro="RREP from Node3";									
									jp.showMessageDialog(jp,pro);
									st2.executeUpdate("insert into a1 values(1,0,1,0)");
																		
								}
						
					 
					}
	else if(query2.equals("Node4"))  
					{
					
							System.out.println("value is"+k);
							if(k==0)
							{
								
									Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
									Connection con=DriverManager.getConnection("jdbc:odbc:a1");
									Statement st1=con.createStatement();
									Statement st2=con.createStatement();
									ResultSet rs=st1.executeQuery("select * from a1");
									while(rs.next())
									{
										valu=rs.getString(4);
										val=Integer.parseInt(valu); 
										if(val==1)
										{
											flag=1;
											break;
										}
									}

									if(flag==1)
										{ 
											String process="process is going on";
											JOptionPane jp=new JOptionPane();
											jp.showMessageDialog(jp,process);
											System.out.println("value of :" +valu);									
										}
									else 
										{
											String pro="RREQ to Node2";
											JOptionPane jp=new JOptionPane();
											jp.showMessageDialog(jp,pro);
											Thread.sleep(700);											

											String pro1="RREQ to Node3";											
											jp.showMessageDialog(jp,pro1);
											Thread.sleep(700);

											String pro2="RREP from Node2";											
											jp.showMessageDialog(jp,pro2);
											st2.executeUpdate("insert into a1 values(1,1,0,1)"); 																				
										}
										value=k;
										++k;									
								  
							}
							else  
							{
							   
									Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
									Connection con=DriverManager.getConnection("jdbc:odbc:a1");
									Statement st1=con.createStatement();
									Statement st=con.createStatement();
									ResultSet rs=st1.executeQuery("select * from a1");
									while(rs.next())
									{
										valu=rs.getString(4);
										val=Integer.parseInt(valu); 
										if(val==1)
										{
											flag=1;
											break;
										}
									}
									if(flag==1)
									   {
											String process="process is going on";
											JOptionPane jp=new JOptionPane();
											jp.showMessageDialog(jp,process);
											System.out.println("value of :" +valu);									
									   }
									else 
										{
											String pro1="RREQ to  Node2";
											JOptionPane jp=new JOptionPane();
											jp.showMessageDialog(jp,pro1);
											Thread.sleep(700);

											
											String pro="RREQ to Node3";											
											jp.showMessageDialog(jp,pro);
											Thread.sleep(700);

											String pro2="RREP from Node3";											
											jp.showMessageDialog(jp,pro2);
											st.executeUpdate("insert into a1 values(1,0,1,1)");										
										}
									 if(k==1)
										  {
											value=k;
											--k;									
										  }
								}
						  }
					}
				catch (Exception e)
				{
					e.printStackTrace();
				}
				return value;
		}
		public int retrieve2(String query3) throws java.rmi.RemoteException
			{
				int flag=0;
				try
				{
					if (query3.equals("Node1")) 
					{					
					
										
								Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
								Connection con=DriverManager.getConnection("jdbc:odbc:a1");
								Statement st1=con.createStatement();
								Statement st2=con.createStatement();
								ResultSet rs=st1.executeQuery("select * from a1");
								while(rs.next())
								 {
									valu=rs.getString(1);
									val=Integer.parseInt(valu); 
									if(val==1)
									{
										flag=1;
										break;
									}
								 }
								if(flag==1)
								{
									String process="process is going on";
									JOptionPane jp=new JOptionPane();
									jp.showMessageDialog(jp,process);
									System.out.println("value of :" +valu);								
								}										
								else 
								{
									String pro1="RREQ to Node1";
									JOptionPane jp=new JOptionPane();
									jp.showMessageDialog(jp,pro1);
									Thread.sleep(700);

									String pro="RREP from Node1";									
									jp.showMessageDialog(jp,pro);
									st2.executeUpdate("insert into a1 values(1,1,0,0)");								
								}						
								
					}
					else if  (query3.equals("Node4")) 
					{
					
							Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
							Connection con=DriverManager.getConnection("jdbc:odbc:a1");
							Statement st1=con.createStatement();
							Statement st2=con.createStatement();
							ResultSet rs=st1.executeQuery("select * from a1");
							while(rs.next())
							{
								valu=rs.getString(4);
								val=Integer.parseInt(valu); 								
								if(val==1)
								{
									flag=1;
									break;
								}
							}
							   if(flag==1)
								{
									String process="process is going on";
									JOptionPane jp=new JOptionPane();
									jp.showMessageDialog(jp,process);
									System.out.println("value of :" +valu);								
								}
								else 
								{
									String pro="RREQ to Node4";
									JOptionPane jp=new JOptionPane();
									jp.showMessageDialog(jp,pro);
									Thread.sleep(700);

									String pro1="RREP from Node4";									
									jp.showMessageDialog(jp,pro1);
									st2.executeUpdate("insert into a1 values(0,1,0,1)");					   
									
								}
						
					 
					}	

					else if(query3.equals("Node3"))  
					{				
							System.out.println("value is"+k);
							if(k==0)
							{
								
									Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
									Connection con=DriverManager.getConnection("jdbc:odbc:a1");
									Statement st1=con.createStatement();
									Statement st2=con.createStatement();
									ResultSet rs=st1.executeQuery("select * from a1");
									while(rs.next())
									{
										valu=rs.getString(3);
										val=Integer.parseInt(valu); 
										if(val==1)
										{
											flag=1;
											break;
										}
									}

									if(flag==1)
										{ 
											String process="process is going on";
											JOptionPane jp=new JOptionPane();
											jp.showMessageDialog(jp,process);
											System.out.println("value of :" +valu);									
										}
									else 
										{
											String pro="RREQ to Node1";
											JOptionPane jp=new JOptionPane();
											jp.showMessageDialog(jp,pro);
											Thread.sleep(700);

											String pro1="RREQ to Node4";											
											jp.showMessageDialog(jp,pro1);
											Thread.sleep(700);

											String pro2="RREP From Node4";											
											jp.showMessageDialog(jp,pro2);											
											st2.executeUpdate("insert into a1 values(0,1,1,1)");																			
										}
										value=k;
										++k;
										
								  
							}
							else  
							{
							   
									Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
									Connection con=DriverManager.getConnection("jdbc:odbc:a1");
									Statement st1=con.createStatement();
									Statement st=con.createStatement();
									ResultSet rs=st1.executeQuery("select * from a1");
									while(rs.next())
									{
										valu=rs.getString(3);
										val=Integer.parseInt(valu); 
										if(val==1)
										{
											flag=1;
											break;
										}
									}
									if(flag==1)
									   {
											String process="process is going on";
											JOptionPane jp=new JOptionPane();
											jp.showMessageDialog(jp,process);
											System.out.println("value of :" +valu);									
									   }
									else 
										{
											String pro="RREQ to Node1";
											JOptionPane jp=new JOptionPane();
											jp.showMessageDialog(jp,pro);
											Thread.sleep(700);

											String pro1="RREQ to Node4";											
											jp.showMessageDialog(jp,pro1);
											Thread.sleep(700);

											String pro2="RREP From Node1";											
											jp.showMessageDialog(jp,pro2);											
											st.executeUpdate("insert into a1 values(1,1,1,0)");										
										}
									 if(k==1)
										  {
											value=k;
											--k;									
										  }
								}
						  }
					}
				catch (Exception e)
				{
					e.printStackTrace();
				}
				return value;
		}

		public int retrieve3(String query4) throws java.rmi.RemoteException

			{
				int flag=0;
				try
				{
					if (query4.equals("Node1")) 
					{	
					
										
								Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
								Connection con=DriverManager.getConnection("jdbc:odbc:a1");
								Statement st1=con.createStatement();
								Statement st2=con.createStatement();
								ResultSet rs=st1.executeQuery("select * from a1");
								while(rs.next())
								 {
									valu=rs.getString(1);
									val=Integer.parseInt(valu); 
									if(val==1)
									{
										flag=1;
										break;
									}
								 }
								if(flag==1)
								{
									String process="process is going on";
									JOptionPane jp=new JOptionPane();
									jp.showMessageDialog(jp,process);
									System.out.println("value of :" +valu);								
								}										
								else 
								{
									String pro="RREQ to Node1";
									JOptionPane jp=new JOptionPane();
									jp.showMessageDialog(jp,pro);
									Thread.sleep(700);

									String pro1="RREP from Node1";
									jp.showMessageDialog(jp,pro1);
									st2.executeUpdate("insert into a1 values(1,0,1,0)");								
								}
										
						
								
					}
					else if  (query4.equals("Node4")) 
					{
					
							Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
							Connection con=DriverManager.getConnection("jdbc:odbc:a1");
							Statement st1=con.createStatement();
							Statement st2=con.createStatement();
							ResultSet rs=st1.executeQuery("select * from a1");
							while(rs.next())
							{
								valu=rs.getString(4);
								val=Integer.parseInt(valu); 
								JOptionPane jp=new JOptionPane();
								jp.showMessageDialog(jp,val);
								if(val==1)
								{
									flag=1;
									break;
								}
							}
							   if(flag==1)
								{
									String process="process is going on";
									JOptionPane jp=new JOptionPane();
									jp.showMessageDialog(jp,process);
									System.out.println("value of :" +valu);								
								}
								else 
								{
									String pro="RREQ to Node4";
									JOptionPane jp=new JOptionPane();
									jp.showMessageDialog(jp,pro);
									Thread.sleep(700);

									String pro1="RREP from Node4";									
									jp.showMessageDialog(jp,pro1);
									st2.executeUpdate("insert into a1 values(0,0,1,1)");					   
									
								}
						
					 
					}	

					else if(query4.equals("Node2"))  
					{				
							System.out.println("value is"+k);
							if(k==0)
							{
								
									Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
									Connection con=DriverManager.getConnection("jdbc:odbc:a1");
									Statement st1=con.createStatement();
									Statement st2=con.createStatement();
									ResultSet rs=st1.executeQuery("select * from a1");
									while(rs.next())
									{
										valu=rs.getString(2);
										val=Integer.parseInt(valu); 
										if(val==1)
										{
											flag=1;
											break;
										}
									}

									if(flag==1)
										{ 
											String process="process is going on";
											JOptionPane jp=new JOptionPane();
											jp.showMessageDialog(jp,process);
											System.out.println("value of :" +valu);									
										}
									else 
										{
											String pro="RREQ to Node1";
											JOptionPane jp=new JOptionPane();
											jp.showMessageDialog(jp,pro);
											Thread.sleep(700);

											String pro1="RREQ to Node4";											
											jp.showMessageDialog(jp,pro1);
											Thread.sleep(700);

											String pro2="RREP From Node1";											
											jp.showMessageDialog(jp,pro2);		
											st2.executeUpdate("insert into a1 values(1,1,1,0)");					   
																				
										}
										value=k;
										++k;
										
								  
							}
							else  
							{							   
									Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
									Connection con=DriverManager.getConnection("jdbc:odbc:a1");
									Statement st1=con.createStatement();
									Statement st=con.createStatement();
									ResultSet rs=st1.executeQuery("select * from a1");
									while(rs.next())
									{
										valu=rs.getString(2);
										val=Integer.parseInt(valu); 
										if(val==1)
										{
											flag=1;
											break;
										}
									}
									if(flag==1)
									   {
											String process="process is going on";
											JOptionPane jp=new JOptionPane();
											jp.showMessageDialog(jp,process);
											System.out.println("value of :" +valu);									
									   }
									else 
										{
											String pro="RREQ to Node1";
											JOptionPane jp=new JOptionPane();
											jp.showMessageDialog(jp,pro);
											Thread.sleep(700);

											String pro1="RREQ to Node4";											
											jp.showMessageDialog(jp,pro1);
											Thread.sleep(700);

											String pro2="RREP From Node4";											
											jp.showMessageDialog(jp,pro2);		
											st.executeUpdate("insert into a1 values(0,1,1,1)");					   
											
										}
									 if(k==1)
										  {
											value=k;
											--k;									
										  }
								}
						  }
					}
				catch (Exception e)
				{
					e.printStackTrace();
				}
				return value;
		}


		public int retrieve4(String query5) throws java.rmi.RemoteException
			{
				int flag=0;
				try
				{
					if (query5.equals("Node2")) 
					{					
					
										
								Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
								Connection con=DriverManager.getConnection("jdbc:odbc:a1");
								Statement st1=con.createStatement();
								Statement st2=con.createStatement();
								ResultSet rs=st1.executeQuery("select * from a1");
								while(rs.next())
								 {
									valu=rs.getString(2);
									val=Integer.parseInt(valu); 
									if(val==1)
									{
										flag=1;
										break;
									}
								 }
								if(flag==1)
								{
									String process="process is going on";
									JOptionPane jp=new JOptionPane();
									jp.showMessageDialog(jp,process);
									System.out.println("value of :" +valu);								
								}										
								else 
								{
									String pro="RREQ to Node2";
									JOptionPane jp=new JOptionPane();
									jp.showMessageDialog(jp,pro);
									Thread.sleep(700);

									String pro1="RREP from Node2";									
									jp.showMessageDialog(jp,pro);
									st2.executeUpdate("insert into a1 values(0,1,0,1)");					   
									
								}
										
						
								
					}
					else if  (query5.equals("Node3")) 
					{
					
							Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
							Connection con=DriverManager.getConnection("jdbc:odbc:a1");
							Statement st1=con.createStatement();
							Statement st2=con.createStatement();
							ResultSet rs=st1.executeQuery("select * from a1");
							while(rs.next())
							{
								valu=rs.getString(3);
								val=Integer.parseInt(valu); 								
								if(val==1)
								{
									flag=1;
									break;
								}
							}
							   if(flag==1)
								{
									String process="process is going on";
									JOptionPane jp=new JOptionPane();
									jp.showMessageDialog(jp,process);
									System.out.println("value of :" +valu);								
								}
								else 
								{

									String pro1="RREQ to Node3";
									JOptionPane jp=new JOptionPane();
									jp.showMessageDialog(jp,pro1);
									Thread.sleep(700);

									String pro="RREP from Node3";									
									jp.showMessageDialog(jp,pro);

									st2.executeUpdate("insert into a1 values(0,0,1,1)");					   
									
								}
						
					 
					}	

					else if(query5.equals("Node1"))  
					{				
							System.out.println("value is"+k);
							if(k==0)
							{
								
								Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
								Connection con=DriverManager.getConnection("jdbc:odbc:a1");
								Statement st1=con.createStatement();
								Statement st2=con.createStatement();
								ResultSet rs=st1.executeQuery("select * from a1");
								    while(rs.next())
									{
										valu=rs.getString(1);
										val=Integer.parseInt(valu); 
										if(val==1)
										{
											flag=1;
											break;
										}
									}

									if(flag==1)
										{ 
											String process="process is going on";
											JOptionPane jp=new JOptionPane();
											jp.showMessageDialog(jp,process);
											System.out.println("value of :" +valu);									
										}
									else 
										{
											String pro="RREQ to Node2";
											JOptionPane jp=new JOptionPane();
											jp.showMessageDialog(jp,pro);
											Thread.sleep(700);

											String pro1="RREQ to Node3";											
											jp.showMessageDialog(jp,pro1);
											Thread.sleep(700);

											String pro2="RREP From Node3";											
											jp.showMessageDialog(jp,pro2);
											st2.executeUpdate("insert into a1 values(1,0,1,1)");   
																				
										}
										value=k;
										++k;
										
								  
							}
							else  
							{							   
									Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
									Connection con=DriverManager.getConnection("jdbc:odbc:a1");
									Statement st1=con.createStatement();
									Statement st=con.createStatement();
									ResultSet rs=st1.executeQuery("select * from a1");
									while(rs.next())
									{
										valu=rs.getString(1);
										val=Integer.parseInt(valu); 
										if(val==1)
										{
											flag=1;
											break;
										}
									}
									if(flag==1)
									   {
											String process="process is going on";
											JOptionPane jp=new JOptionPane();
											jp.showMessageDialog(jp,process);
											System.out.println("value of :" +valu);									
									   }
									else 
										{
											String pro="RREQ to Node2";
											JOptionPane jp=new JOptionPane();
											jp.showMessageDialog(jp,pro);
											Thread.sleep(700);

											String pro1="RREQ to Node3";											
											jp.showMessageDialog(jp,pro1);
											Thread.sleep(700);

											String pro2="RREP From Node2";											
											jp.showMessageDialog(jp,pro2);
											st.executeUpdate("insert into a1 values(1,1,0,1)");					   
											
										}
									 if(k==1)
										  {
											value=k;
											--k;									
										  }
								  }
						   }
					}
				catch (Exception e)
				{
					e.printStackTrace();
				}
				return value;
		}


}
